<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./dist/echarts.js"></script>
    <style>
        body {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        .container {
            border: 1px solid #eee;
            width: 100vw;
            height: 100vh;
        }

        .box1 {
            background: skyblue;
        }

        .box2 {
            background: pink;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="box2" draggable="true">
            <div id="chart2" style="height: 300px; width: 400px;"></div>
        </div>
        <div class="box1" draggable="true">
            <div id="chart1" style="height: 300px; width: 400px;"></div>
        </div>

    </div>
</body>
<script>
    function initChart(el) {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(el);

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
                data: ['销量']
            },
            xAxis: {
                data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
            },
            yAxis: {},
            series: [
                {
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }
            ]
        };
        myChart.setOption(option)
    }

    function initChart1() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.querySelector('#chart1'));

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
                data: ['销量']
            },
            xAxis: {
                data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
            },
            yAxis: {},
            series: [
                {
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }
            ]
        };
        myChart.setOption(option)

        return myChart
    }

    function initChart2() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.querySelector('#chart2'));

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
                data: ['销量']
            },
            xAxis: {
                data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
            },
            yAxis: {},
            series: [
                {
                    name: '销量',
                    type: 'line',
                    data: [5, 20, 36, 10, 10, 20]
                }
            ]
        };
        myChart.setOption(option)
    }

    const myChart1 = initChart1()
    initChart2()

    const container = document.querySelector('.container')
    const box1 = document.querySelector('.box1');
    const box2 = document.querySelector('.box2');

    let copyNode = null
    let sourceNode = null

    box1.ondrag = function (e) {
        sourceNode = e.target
        copyNode = e.target.cloneNode(true)
    }
    box2.ondrag = function (e) {
        sourceNode = e.target
        copyNode = e.target.cloneNode(true)
    }

    container.ondragover = function (e) {
        e.preventDefault()
    }

    box2.ondrop = function (e) {
        e.preventDefault()
        e.target.parentNode.insertBefore(copyNode, e.target)
        sourceNode.remove()
        const chart = copyNode.querySelector('#chart1')
        chart.removeAttribute('_echarts_instance_')
        initChart(chart)
    }

    box1.onclick = function () {
        console.log('click')
    }
    box1.ondrop = function (e) {
        e.preventDefault()
        e.target.parentNode.insertBefore(copyNode, e.target)
        sourceNode.remove()
        const chart = copyNode.querySelector('#chart2')
        chart.removeAttribute('_echarts_instance_')
        initChart2()
    }
</script>

<script>

</script>

<script>

</script>

</html>